var util = require('../../utils/util.js')
const api = require('../../utils/api.js')
const user = require('../index/model.js')
const {
  $Message
} = require('../../dist/base/index');
Page({
    data : {
        scrollTop : 0,
        spinShow:false,
        province:[],
        contact_book:[],
        tempPro:[],
        tempProPerson:[],
        tempProPersonObj:[]
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var self = this
        this.setData({
            spinShow:true
        })
        api.getHistory(0, [],"province","name","desc",{},function(provinceArr){
            self.setData({
                province:provinceArr
            })
            console.log(provinceArr)
            api.getHistory(0, [],"contact_book","name","desc",{},function(contact_book){
                //数组处理
                var tempPro = []
                var tempProPerson = []
                var tempProPersonObj = []
                provinceArr.forEach(element => {
                    tempPro.push(element.name)
                    var tempSingelProPerson = []
                    var tempSingelProPersonObj = []
                    contact_book.forEach(cb => {
                        if(cb.province_id == element._id){
                            tempSingelProPerson.push(cb.name)
                            tempSingelProPersonObj.push(cb)
                        }
                    });
                    tempProPersonObj.push(tempSingelProPersonObj)
                    tempProPerson.push(tempSingelProPerson)
                });
                console.log('------')
                console.log(tempPro)
                console.log(tempProPerson)
                self.setData({
                    tempPro:tempPro,
                    tempProPerson:tempProPerson,
                    tempProPersonObj:tempProPersonObj,
                    spinShow:false,
                    contact_book:contact_book
                })
                
            })
        })
    },
    onChange(event){
        console.log(event.detail,'click right menu callback data')
    },
    //页面滚动执行方式
    onPageScroll(event){
        this.setData({
            scrollTop : event.scrollTop
        })
    },
    addContact(){
        wx.navigateTo({
          url: '/pages/contactPage/addContact/index',
        })
    },
    click_call(e){
        var idArr = e.currentTarget.id.split('-')
        wx.makePhoneCall({
          phoneNumber: this.data.tempProPersonObj[parseInt(idArr[0])][parseInt(idArr[1])].phone,
        })
    }
});